package com.fejal.dao;

import java.util.List;

import com.fejal.bean.GradeCurricular;
import com.fejal.bean.GradeDisciplina;

public class GradeCurricularDAOHibernate implements GradeCurricularDAO {

	HibernateDAOFactory hibernateDAOFactory = null;
	HibernateDAO<GradeCurricular> hibernateDAO = null;
	
	public GradeCurricularDAOHibernate(){
		hibernateDAOFactory = new HibernateDAOFactory();
		hibernateDAO = hibernateDAOFactory.getGradeCurricularDao();
	}
	
	@Override
	public boolean inserir(GradeCurricular gradeCurricular) throws Exception{

		boolean ok = false;

		try {
			hibernateDAO.add(gradeCurricular);
			ok = true;

		} catch (Exception e) {
			throw new Exception(e.getMessage());
		} 

		return ok;
	}	
	
	@Override
	public boolean alterar(GradeCurricular gradeCurricular) throws Exception{
		boolean ok = false;
		
		try {
			hibernateDAO.update(gradeCurricular);

			ok = true;

		} catch (Exception e) {

			throw new Exception(e.getMessage());
		} 

		return ok;
		

	}

	@Override
	public boolean excluir(GradeCurricular gradeCurricular) throws Exception{
		
		boolean ok = false;
		
		try {
			hibernateDAO.remove(gradeCurricular);
			ok = true;
		} catch (Exception e) {

			throw new Exception(e.getMessage());
		} 

		return ok;
	}

	@Override
	public GradeCurricular getGradeCurricular(int codigoGradeCurricular) {
		GradeCurricular gradeCurricular = hibernateDAO.get(codigoGradeCurricular);
		return gradeCurricular;
	}

	@Override
	public List<GradeCurricular> getListTodos() {
		List<GradeCurricular> list = hibernateDAO.listAll();
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GradeCurricular> getSelecionar(String descricao, int ano, int semestre, int codigoCurso) {

		String 	sql = 	"SELECT grade FROM 	GradeCurricular AS grade ";


		if(!(codigoCurso == 0)){
			sql += " INNER	JOIN  grade.cursos as curso";
		}

		sql +=	"	WHERE grade.codigoGradeCurricular <> 0 ";

		if (!(descricao.isEmpty())) {
			sql += "AND grade.descricao like '" + descricao + "%' ";  
		}

		if (!(ano == 0)) {
			sql += "AND grade.ano = '" + ano + "' ";  
		}

		if (!(semestre == 0)) {
			sql += "AND grade.semestre = '" + semestre + "' ";  
		}

		if (!(codigoCurso == 0)) {
			sql += "AND curso.codigoCurso = '" + codigoCurso + "' ";  
		}	

		sql += "ORDER BY grade.descricao";		
 
		List<GradeCurricular> lista = hibernateDAO.getSession().createQuery(sql).list();

		return lista;
	}

	@Override
	public boolean isExiste(GradeCurricular gradeCurricular) {
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GradeDisciplina> getGradeDisciplinas(int codigoGradeCurricular, int periodo) {

		String 	sql = 	"SELECT gradeDisciplinas " +
						"FROM GradeCurricular as grade " +
						"INNER JOIN grade.gradeDisciplinas as gradeDisciplinas " +
						"WHERE grade.codigoGradeCurricular = "+codigoGradeCurricular+ 
						"  AND periodo = "+periodo ;

		sql += 			" ORDER BY gradeDisciplinas.disciplina.descricao";		
 
		List<GradeDisciplina> lista = hibernateDAO.getSession().createQuery(sql).list();
		return lista;
	}
	
	@SuppressWarnings("unused")
	@Override
	public void inicialize(GradeCurricular gradeCurricular){
		if (gradeCurricular != null && gradeCurricular.getGradeDisciplinas()!= null){
			GradeCurricular grade = DAOFactory.getGradeCurricularDAO().getGradeCurricular(gradeCurricular.getCodigoGradeCurricular());
			
			for (GradeDisciplina gradeDisciplina : grade.getGradeDisciplinas()){
				for (GradeDisciplina gradeDisciplinaPreRequisito : gradeDisciplina.getPreRequisitos()){
				}
				gradeDisciplina.setPreRequisitos(gradeDisciplina.getPreRequisitos());
			}
			gradeCurricular.setGradeDisciplinas(grade.getGradeDisciplinas());
			gradeCurricular.setCursos(grade.getCursos());
		}
	}
	
}
